using System;
using System.Collections.Generic;
using CxRomos.Core.DataAccess;
using CxRomos.Core.Services;
using CxRomos.Core.Services.Implementation;
using CxRomos.Web.Account.Interface;
using StructureMap;

namespace CxRomos.Web.Account.Presenter {
    public class RegisterPresenter {
        private IRegister _view;
        private IAccountRepository _accountRepository;
        private IPermissionRepository _permissionRepository;
        private ITermRepository _termRepository;
        private IAccountService _accountService;
        private IWebContext _webContext;
        private IEmail _email;
        private IConfiguration _configuration;

        public void Init(IRegister view) {
            _view = view;
            _accountRepository = ObjectFactory.GetInstance<IAccountRepository>();
            _permissionRepository = ObjectFactory.GetInstance<IPermissionRepository>();
            _termRepository = ObjectFactory.GetInstance<ITermRepository>();
            _accountService = ObjectFactory.GetInstance<IAccountService>();
            _webContext = ObjectFactory.GetInstance<IWebContext>();
            _email = ObjectFactory.GetInstance<IEmail>();
            _configuration = ObjectFactory.GetInstance<IConfiguration>();

            _view.LoadTerms(_termRepository.GetCurrentTerm());
        }

        public void Register(string firstName, string lastName, string email, string password) {
            var account = new Core.Domain.Entities.Account {
                                                               FirstName = firstName,
                                                               LastName = lastName,
                                                               Email = email,
                                                               Password =
                                                               Cryptography.Encrypt(password, email.Split('@')[0]),
                                                               CreateDate = DateTime.Now,
                                                               LastUpdateDate = DateTime.Now,
                                                               BirthDate = new DateTime(1974, 9, 6),
                                                               EmailVerified = true
                                                           };

            if(_accountService.EmailInUse(email)) {
                _view.ShowErrorMessage("This email is already in use");
            }
            else {
                _accountRepository.SaveAccount(account);
                List<Core.Domain.Entities.Permission> permissions = _permissionRepository.GetPermissionByName("PUBLIC");
                Core.Domain.Entities.Account newAccount = _accountRepository.GetAccountByEmail(email);

                if(permissions.Count > 0 && newAccount != null) {
                    _accountRepository.AddPermission(newAccount, permissions[0]);
                }

                //_email.SendEmailAddressVerificationEmail(email.Split('@')[0], email);
                _view.ShowAccountCreatedPanel();
            }

        }

        public void LoginLinkClicked() {
            throw new NotImplementedException();
        }
    }
}